package com.example.wallpaperapp.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


import com.example.wallpaperapp.R;
import com.example.wallpaperapp.adapter.StaggeredGridAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class WeekFragment extends Fragment implements View.OnClickListener {
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout refreshLayout;
    private List<Integer> lists = new ArrayList<>();
    private StaggeredGridAdapter adapter;
//    private ImageView mback;

    private int[] imgs = {
            R.drawable.img_1, R.drawable.img_2,R.drawable.img_3,
            R.drawable.img_4, R.drawable.image0,R.drawable.img_5,
            R.drawable.img_6, R.drawable.img_4,R.drawable.img_4,
            R.drawable.img_5, R.drawable.img_5, R.drawable.img_4,
            R.drawable.img_5, R.drawable.img_5, R.drawable.img_5,
    };
    public WeekFragment() {

    }
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_paihang,null);
//        mback= view.findViewById(R.id.back);
//        mback.setOnClickListener(this);

        refreshLayout=view.findViewById(R.id.refreshlayout);
        mRecyclerView=view.findViewById(R.id.rv_pu);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(null);
        initPager();
        return view;

    }

    private void initPager() {
        //垂直方向的2列
        final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));//设置RecyclerView布局管理器为2列垂直排布
        //防止Item切换
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        mRecyclerView.setLayoutManager(layoutManager);
        final int spanCount=3;
        //解决底部滚动到顶部时，顶部item上方偶尔会出现一大片间隔的问题
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                int[] first = new int[spanCount];
                layoutManager.findFirstCompletelyVisibleItemPositions(first);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && (first[0] == 1 || first[1] == 1)) {
                    layoutManager.invalidateSpanAssignments();
                }
            }
        });

        for (int i = 0; i < imgs.length; i++) {
            lists.add(imgs[i]);
        }
        adapter=new StaggeredGridAdapter();
        mRecyclerView.setAdapter(adapter);
        adapter.replaceAll((ArrayList<Integer>) lists);
        //设置下拉刷新和上拉加载监听
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.replaceAll(getData());
                        refreshLayout.finishRefresh();
                    }
                }, 1000);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.addData(adapter.getItemCount(), getData());
                        refreshLayout.finishLoadMore();
                    }
                }, 1000);
            }
        });
    }

    private ArrayList<Integer> getData(){
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0 ; i < 12;i++){
            list.add(imgs[i]);
        }
        return list;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.back:
//                finish();
//                break;
            default:
                break;
        }
    }

}
